﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace QLNSClient
{
    public partial class uc_ThemCanbo : UserControl
    {

        QLNSServiceReference.QLNSServiceClient sv = new QLNSServiceReference.QLNSServiceClient();

        public static Byte[] ReadImageFromFile(string ImagePath)
        {
            Byte[] ImageData = null;  //khai bao mang ImageData kieu byte luu tru anh
            FileInfo fi = new FileInfo(ImagePath); //      khai bao bien fi kieu FileInfo de doc thong tin ImagePath
            long NumberOfBytes = fi.Length; // khai bao bien NumberOfBytes kieu Long de luu do dai cua bien fi
            FileStream fs = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);         // bien br doc bien fs
            ImageData = br.ReadBytes((int)NumberOfBytes);
            return ImageData;
        }
 
        public uc_ThemCanbo()
        {
            InitializeComponent();
        }


        public void LoadData()
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = false;

            btn_themAnh.Enabled = false;
            //btn_xoaAnh.Enabled = false;

            txt_Ma_CB.Enabled = false;
            txt_Hoten.Enabled = false;
            txt_Tengoikhac.Enabled = false;
            rdg_Gioitinh.Enabled = false;
            de_Namsinh.Enabled = false;
            txt_Noisinh.Enabled = false;
            txt_Quequan.Enabled = false;
            txt_SoCMND.Enabled = false;
            de_NgaycapCMND.Enabled = false;
            txt_Noicap.Enabled = false;
            txt_SoBHXH.Enabled = false;
            rdg_Thuongbinh.Enabled = false;
            txt_Hokhau.Enabled = false;
            txt_Choo.Enabled = false;
            lke_Dantoc.Enabled = false;
            lke_Tongiao.Enabled = false;
            txt_Suckhoe.Enabled = false;
            txt_Nghenghiep.Enabled = false;
            de_Ngaytuyendung.Enabled = false;
            lke_DV.Enabled = false;
            rdg_Dangvien.Enabled = false;
            rdg_Nhapngu.Enabled = false;


            rdg_Gioitinh.SelectedIndex = 0;

            rdg_Thuongbinh.SelectedIndex = 0;

            rdg_Dangvien.SelectedIndex = 1;

            rdg_Nhapngu.SelectedIndex = 1;

            lke_Gdchinhsach.Enabled = false;

            lke_Dantoc.Properties.DataSource = sv.getDT();

            lke_Tongiao.Properties.DataSource = sv.getTG();

            lke_DV.Properties.DataSource = sv.getDMDV();

            lke_Gdchinhsach.Properties.DataSource = sv.getGDCS();

            gv_dsCB.DataSource = sv.getCB();

            gridView1.Columns[1].Caption="Mã cán bộ";
            gridView1.Columns[2].Caption="Tên cán bộ";
            gridView1.Columns[4].Caption="Ngày sinh";
            gridView1.Columns[5].Caption="Giới tính";
            gridView1.Columns[6].Caption="Nơi sinh";
            gridView1.Columns[10].Caption="Nghề nghiệp";
            gridView1.Columns[11].Caption="Ngày tuyển dụng";
            gridView1.Columns[12].Caption="Đảng viên";
            gridView1.Columns[13].Caption="Nhập ngũ";
            gridView1.Columns[23].Caption = "Mã đơn vị";

            gridView1.Columns[0].Visible = false;
            gridView1.Columns[3].Visible = false;
            gridView1.Columns[7].Visible = false;
            gridView1.Columns[8].Visible = false;
            gridView1.Columns[9].Visible = false;
            gridView1.Columns[21].Visible = false;
            gridView1.Columns[14].Visible = false;
            gridView1.Columns[15].Visible = false;
            gridView1.Columns[16].Visible = false;
            gridView1.Columns[17].Visible = false;
            gridView1.Columns[18].Visible = false;
            gridView1.Columns[19].Visible = false;
            gridView1.Columns[20].Visible = false;
            gridView1.Columns[22].Visible = false;
 
        }


         
        private void btn_Them_Click(object sender, EventArgs e)
        {
            txt_Ma_CB.Enabled = false;

            List<QLNSServiceReference.CAN_BO> listLastMa_CB = new List<QLNSServiceReference.CAN_BO>(sv.getLastMa_CB());
            string lastID = listLastMa_CB[0].Ma_CB;
            int vt = lastID.IndexOf("B");
            string LastNum = lastID.Substring(vt+1,4);
            int tempID = Int32.Parse(LastNum);
            int NumNewID = tempID + 1;
    

            string NewID;
            if (NumNewID < 10)
            {
                NewID = "CB000" + NumNewID.ToString();
                txt_Ma_CB.Text = NewID;
            }
            if (NumNewID < 100 && NumNewID >= 10)
            {
                NewID = "CB00" + NumNewID.ToString();
                txt_Ma_CB.Text = NewID;
            }
            if (NumNewID < 1000 && NumNewID >= 100)
            {
                NewID = "CB0" + NumNewID.ToString();
                txt_Ma_CB.Text = NewID;
            }
            if (NumNewID >= 1000)
            {
                NewID = "CB" + NumNewID.ToString();
                txt_Ma_CB.Text = NewID;
            }
                        
                       
            
            //demtime = 1;
            
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = true;

            btn_themAnh.Enabled = true;
            //btn_xoaAnh.Enabled = true;

            //txt_Ma_CB.Enabled = true;
            txt_Hoten.Enabled = true;
            txt_Tengoikhac.Enabled = true;
            rdg_Gioitinh.Enabled = true;
            de_Namsinh.Enabled = true;
            txt_Noisinh.Enabled = true;
            txt_Quequan.Enabled = true;
            txt_SoCMND.Enabled = true;
            de_NgaycapCMND.Enabled = true;
            txt_Noicap.Enabled = true;
            txt_SoBHXH.Enabled = true;
            rdg_Thuongbinh.Enabled = true;
            txt_Hokhau.Enabled = true;
            txt_Choo.Enabled = true;
            lke_Dantoc.Enabled = true;
            lke_Tongiao.Enabled = true;
            txt_Suckhoe.Enabled = true;
            txt_Nghenghiep.Enabled = true;
            de_Ngaytuyendung.Enabled = true;
            lke_DV.Enabled = true;
            rdg_Dangvien.Enabled = true;
            rdg_Nhapngu.Enabled = true;
            //lke_Gdchinhsach.Enabled = true;
            ck_coGDCS.Enabled = true;
            ck_coGDCS.Checked = false;
            lke_Gdchinhsach.EditValue = null;

            //txt_Ma_CB.Text = "";
            txt_Hoten.Text = "";
            txt_Tengoikhac.Text = "";
            rdg_Gioitinh.SelectedIndex = 0;
            de_Namsinh.Text = "";
            txt_Noisinh.Text = "";
            txt_Quequan.Text = "";
            txt_SoCMND.Text = "";
            de_NgaycapCMND.Text = "";
            txt_Noicap.Text = "";
            txt_SoBHXH.Text = "";
            rdg_Thuongbinh.SelectedIndex = 0;
            txt_Hokhau.Text = "";
            txt_Choo.Text = "";
            lke_Dantoc.EditValue = null;
            lke_Tongiao.EditValue = null;
            txt_Suckhoe.Text = "";
            txt_Nghenghiep.Text = "";
            de_Ngaytuyendung.Text = "";
            lke_DV.EditValue = null;
            rdg_Dangvien.SelectedIndex = 0;
            rdg_Nhapngu.SelectedIndex = 0;
            
            txt_Hoten.BackColor = Color.Empty;
            txt_Tengoikhac.BackColor = Color.Empty;
            txt_Noisinh.BackColor = Color.Empty;
            txt_Quequan.BackColor = Color.Empty;
            txt_SoCMND.BackColor = Color.Empty;
            txt_Noicap.BackColor = Color.Empty;
            txt_SoBHXH.BackColor = Color.Empty;
            txt_Hokhau.BackColor = Color.Empty;
            txt_Choo.BackColor = Color.Empty;
            txt_Suckhoe.BackColor = Color.Empty;
            txt_Nghenghiep.BackColor = Color.Empty;



            
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            btn_Huy.Enabled = false;
            btn_Luu.Enabled = false;
            btn_Them.Enabled = true;
            
            
                string tempGDCS;
                if (ck_coGDCS.Checked == false)
                {
                    tempGDCS = "0";
                }
                else
                {
                    tempGDCS = lke_Gdchinhsach.EditValue.ToString();
                }

                string tempDangvien = rdg_Dangvien.EditValue.ToString();

                string tempNhapngu = rdg_Nhapngu.EditValue.ToString();

                string tempMaCB = txt_Ma_CB.Text;
                if (txt_Hoten.Text == "")
                {
                    MessageBox.Show("Vui lòng nhập tên!","CẢNH BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {

                    string tempHoTen = txt_Hoten.Text;
                    string tempTenKhac = txt_Tengoikhac.Text;
                    string tempGioitinh = rdg_Gioitinh.EditValue.ToString();
                    //de_Namsinh.DateTime = DateTime.Today;
                    DateTime tempNamsinh;
                    if (de_Namsinh.Text == "")
                    {
                        tempNamsinh = DateTime.Today;
                    }
                    else
                    {
                        tempNamsinh = de_Namsinh.DateTime;
                    }
                    string tempNoisinh = txt_Noisinh.Text;
                    string tempQuequan = txt_Quequan.Text;
                    string tempSoCMND = txt_SoCMND.Text;
                    DateTime tempNgaycap;
                    if (de_NgaycapCMND.Text == "")
                    {
                        tempNgaycap = DateTime.Today;
                    }
                    else
                    {
                        tempNgaycap = de_NgaycapCMND.DateTime;
                    }
                    string tempNoicapCMND = txt_Noicap.Text;
                    string tempSoBHXH = txt_SoBHXH.Text;

                    string tempDantoc;
                    
                    if (lke_Dantoc.EditValue != null)
                    {
                        tempDantoc = lke_Dantoc.EditValue.ToString();

                    }
                    else
                    {

                        tempDantoc = null;
                    }
                    string tempTongiao;

                    if (lke_Tongiao.EditValue != null)
                    {
                        tempTongiao = lke_Tongiao.EditValue.ToString();
                    }
                    else
                    {
                        tempTongiao = null;
                    }
                    string tempSuckhoe = txt_Suckhoe.Text;
                    string tempThuongbinh = rdg_Thuongbinh.EditValue.ToString();
                    string tempHokhau = txt_Hokhau.Text;
                    string tempChoO = txt_Choo.Text;
                    string tempNghenghiep = txt_Nghenghiep.Text;
                    DateTime tempNgaytuyendung;
                    if(de_Ngaytuyendung.Text=="")
                    {
                        tempNgaytuyendung= DateTime.Today;
                    }
                    else
                    {
                        tempNgaytuyendung = de_Ngaytuyendung.DateTime;
                    }
                    string tempDonvi;
                    if (lke_DV.EditValue==null)
                    {
                        tempDonvi ="DV001";
                        
                    }
                    else
                    {
                        tempDonvi = lke_DV.EditValue.ToString();
                    }
                    //--------------------------------
                    QLNSServiceReference.CAN_BO cb = new QLNSServiceReference.CAN_BO();
                    cb.Ma_CB = tempMaCB;
                    cb.HoTen = tempHoTen;
                    cb.TenKhac = tempTenKhac;
                    cb.GioiTinh = tempGioitinh;
                    cb.NamSinh = tempNamsinh;
                    cb.NoiSinh = tempNoisinh;
                    cb.QueQuan = tempQuequan;
                    cb.SoCMND = tempSoCMND;
                    cb.NgayCap = tempNgaycap;
                    cb.NoiCap = tempNoicapCMND;
                    cb.SoBHXH = tempSoBHXH;
                    if (tempDantoc != null)
                    {
                        cb.Ma_DT = Int32.Parse(tempDantoc);
                    }
                    else
                    {
                        cb.Ma_DT =1;
                    }
                    if (tempTongiao != null)
                    {
                        cb.Ma_TG = Int32.Parse(tempTongiao);
                    }
                    else
                    {
                        cb.Ma_DT = 1;
                    }
                    cb.SucKhoe = tempSuckhoe;
                    cb.ThuongBinh = tempThuongbinh;
                    cb.HoKhau = tempHokhau;
                    cb.Cho_O = tempChoO;
                    cb.NgheNghiep = tempNghenghiep;
                    cb.NgayTuyenDung = tempNgaytuyendung;
                    cb.Ma_DV = tempDonvi;
                    cb.Stt_GD_CS = Int32.Parse(tempGDCS);
                    //---------------------------------------------------------------------
                    if (tempDangvien == "1")
                    {
                        cb.DangVien = true;
                    }
                    else
                    {
                        cb.DangVien = false;
                    }
                    //---------------------------------------------------------------------
                    if (tempNhapngu == "1")
                    {
                        cb.NhapNgu = true;
                    }
                    else
                    {
                        cb.NhapNgu = false;
                    }
                    //---------------------------------------------------------------------
                    if (tempPath != null)
                    {
                        cb.HinhAnh = ReadImageFromFile(tempPath);
                    }
                    else
                    {
                        cb.HinhAnh = new Byte[0];
                    }

                    if (demsua == 1)
                    {
                        DialogResult dr = MessageBox.Show("Bạn có chắc muốn cập nhật thông tin của cán bộ '" + txt_Hoten.Text + "'?", "Cảnh báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                        if (dr == DialogResult.OK)
                        {
                            sv.updateCB(cb);
                            demsua = 0;
                            LoadData();
                            MessageBox.Show("Cập nhật thông tin cán bộ '" + txt_Hoten.Text + "' thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    else
                    {
                        int kq = sv.addCB(cb);
                        if (kq != -1)
                        {
                            LoadData();
                            MessageBox.Show("Thêm cán bộ '"+txt_Hoten.Text+"' có mã '"+txt_Ma_CB.Text+"' thành công!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }


                }
         }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            //btn_Huy.Enabled = true;
            btn_Them.Enabled = true;
            btn_Luu.Enabled = false;

            DialogResult dr = MessageBox.Show("Bạn có chắc muốn xóa dữ liệu này?!", "CẢNH BÁO", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                int kq = sv.deleteCB(macb);
                if (kq != -1)
                {
                    MessageBox.Show("Xóa thành công!");
                    LoadData();
                }
                else
                {
                    MessageBox.Show("Dữ liệu bạn muốn xóa có liên quan đến các trước dữ liệu khác!\n\t\tVui lòng xem lại!");
                }
            }
           
          
        }


        int demsua=0;
        private void btn_Sua_Click(object sender, EventArgs e)
        {

            demsua = 1;
            btn_themAnh.Enabled = true;
            //.Enabled = true;
            btn_Luu.Enabled = true;
            btn_Them.Enabled = false;
            btn_Huy.Enabled = true;
            btn_Sua.Enabled = false;

            txt_Ma_CB.Enabled = false;
            txt_Hoten.Enabled = true;
            txt_Tengoikhac.Enabled = true;
            rdg_Gioitinh.Enabled = true;
            de_Namsinh.Enabled = true;
            txt_Noisinh.Enabled = true;
            txt_Quequan.Enabled = true;
            txt_SoCMND.Enabled = true;
            de_NgaycapCMND.Enabled = true;
            txt_Noicap.Enabled = true;
            txt_SoBHXH.Enabled = true;
            rdg_Thuongbinh.Enabled = true;
            txt_Hokhau.Enabled = true;
            txt_Choo.Enabled = true;
            lke_Dantoc.Enabled = true;
            lke_Tongiao.Enabled = true;
            txt_Suckhoe.Enabled = true;
            txt_Nghenghiep.Enabled = true;
            de_Ngaytuyendung.Enabled = true;
            lke_DV.Enabled = true;
            rdg_Dangvien.Enabled = true;
            rdg_Nhapngu.Enabled = true;

        }



        string tempPath; // filename dung de ma hoa anh
       


        string macb;
        private void gv_dsCB_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = true;
            btn_Sua.Enabled = true;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;
            btn_themAnh.Enabled = false;
            //btn_xoaAnh.Enabled = false;

           
            txt_Ma_CB.Enabled = false;
            txt_Hoten.Enabled = false;
            txt_Tengoikhac.Enabled = false;
            rdg_Gioitinh.Enabled = false;
            de_Namsinh.Enabled = false;
            txt_Noisinh.Enabled = false;
            txt_Quequan.Enabled = false;
            txt_SoCMND.Enabled = false;
            de_NgaycapCMND.Enabled = false;
            txt_Noicap.Enabled = false;
            txt_SoBHXH.Enabled = false;
            rdg_Thuongbinh.Enabled = false;
            txt_Hokhau.Enabled = false;
            txt_Choo.Enabled = false;
            lke_Dantoc.Enabled = false;
            lke_Tongiao.Enabled = false;
            txt_Suckhoe.Enabled = false;
            txt_Nghenghiep.Enabled = false;
            de_Ngaytuyendung.Enabled = false;
            lke_DV.Enabled = false;
            rdg_Dangvien.Enabled = false;
            rdg_Nhapngu.Enabled = false;
            lke_Gdchinhsach.Enabled = false;
            ck_coGDCS.Enabled = false;


            try
            {
                txt_Ma_CB.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]);
                if (gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[2]) == "")
                {
                    txt_Hoten.BackColor = Color.Yellow;
                }
                else
                {
                    txt_Hoten.BackColor = Color.Empty;
                }
                txt_Hoten.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[2]);
                if (gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[3]) == "")
                {
                    txt_Tengoikhac.BackColor = Color.Yellow;
                }
                else
                {
                    txt_Tengoikhac.BackColor = Color.Empty;
                }
                txt_Tengoikhac.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[3]);
                de_Namsinh.DateTime = DateTime.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[4]));
                rdg_Gioitinh.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[5]);
                if (gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[6]) == "")
                {
                    txt_Noisinh.BackColor = Color.Yellow;
                }
                {
                    txt_Noisinh.BackColor = Color.Empty;
                }
                txt_Noisinh.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[6]);
                if (gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[6]) == "")
                {
                    txt_Noisinh.BackColor = Color.Yellow;
                }
                else
                {
                    txt_Noisinh.BackColor = Color.Empty;
                }
                txt_Quequan.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[7]);
                if (gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[7]) == "")
                {
                    txt_Quequan.BackColor = Color.Yellow;
                }
                else
                {
                    txt_Quequan.BackColor = Color.Empty;
                }

                txt_Hokhau.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[8]);
                if (gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[8]) == "")
                {
                    txt_Hokhau.BackColor = Color.Yellow;
                }
                else
                {
                    txt_Hokhau.BackColor = Color.Empty;
                }
                txt_Choo.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[9]);
                if (gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[9]) == "")
                {
                    txt_Choo.BackColor = Color.Yellow;
                }
                else
                {
                    txt_Choo.BackColor = Color.Empty;
                }
                txt_Nghenghiep.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[10]);
                if (gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[10]) == "")
                {
                    txt_Nghenghiep.BackColor = Color.Yellow;
                }
                else
                {
                    txt_Nghenghiep.BackColor = Color.Empty;
                }
                de_Ngaytuyendung.DateTime = DateTime.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[11]));

                string a = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[12]);
                string b = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[13]);
                if (a == "Checked")
                {
                    rdg_Dangvien.SelectedIndex = 0;
                }
                else
                {
                    rdg_Dangvien.SelectedIndex = 1;
                }

                if (b == "Checked")
                {
                    rdg_Nhapngu.SelectedIndex = 0;
                }
                else
                {
                    rdg_Nhapngu.SelectedIndex = 1;
                }


                txt_Suckhoe.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[14]);
                if (gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[14]) == "")
                {
                    txt_Suckhoe.BackColor = Color.Yellow;
                }
                else
                {
                    txt_Suckhoe.BackColor = Color.Empty;
                }
                rdg_Thuongbinh.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[15]);
                
                txt_SoCMND.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[16]);
                if (gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[16]) == "")
                {
                    txt_SoCMND.BackColor = Color.Yellow;
                }
                else
                {
                    txt_SoCMND.BackColor = Color.Empty;
                }
                de_NgaycapCMND.DateTime = DateTime.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[17]));
                txt_Noicap.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[18]);
                if (gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[18]) == "")
                {
                    txt_Noicap.BackColor = Color.Yellow;
             
                }
                else
                {
                    txt_Noicap.BackColor = Color.Empty;
                }
                                              
                txt_SoBHXH.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[19]);
                //MessageBox.Show(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[19]));
                if (gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[19]) == "")
                {
                    txt_SoBHXH.BackColor = Color.Yellow;
                  
                }
                else
                {
                    txt_SoBHXH.BackColor = Color.Empty;
                }
                lke_Dantoc.EditValue = Int32.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[20]));
                lke_Tongiao.EditValue = Int32.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[21]));

                //MessageBox.Show(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[22].ToString()));
                if (Int32.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[22])) != 0)
                {
                    ck_coGDCS.Checked = true;
                    lke_Gdchinhsach.Enabled = true;
                    lke_Gdchinhsach.EditValue = Int32.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[22]));
                }
                else
                {
                    ck_coGDCS.Checked = false;
                    lke_Gdchinhsach.Enabled = false;
                }


                lke_DV.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[23]);

                macb = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[1]);

                //lay anh len picture edit-------------------------------------------------------
                DataTable dt_anh = new DataTable();
                sqlHelper.Fill(dt_anh, "select HinhAnh from CAN_BO where Ma_CB='" + macb + "'");
                Byte[] byteBLOBData = new Byte[0];
                byteBLOBData = (Byte[])(dt_anh.Rows[0][0]);

                if (byteBLOBData.Length != 0)
                {
                    MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
                    pte_AnhCB.Image = Image.FromStream(stmBLOBData);

                }
                else
                {
                    pte_AnhCB.Image = null;
                }
            }
            catch
            { }


        }

        public void uc_ThemCanbo_Load(object sender, EventArgs e)
        {
            LoadData();
            
        }



     

                           
        private void btn_themDMDV_Click(object sender, EventArgs e)
        {
            Frm_ThemDMDV frm = new Frm_ThemDMDV();
            frm.ShowDialog();
            lke_DV.Properties.DataSource = sv.getDMDV();
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            demsua = 0;
            
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;

            txt_Ma_CB.Text = "";
            txt_Hoten.Text = "";
            txt_Tengoikhac.Text = "";
            rdg_Gioitinh.SelectedIndex = 0;
            de_Namsinh.Text = "";
            txt_Noisinh.Text = "";
            txt_Quequan.Text = "";
            txt_SoCMND.Text = "";
            de_NgaycapCMND.Text = "";
            txt_Noicap.Text = "";
            txt_SoBHXH.Text = "";
            rdg_Thuongbinh.SelectedIndex = 0;
            txt_Hokhau.Text = "";
            txt_Choo.Text = "";
            lke_Dantoc.EditValue = null;
            lke_Tongiao.EditValue = null;
            txt_Suckhoe.Text = "";
            txt_Nghenghiep.Text = "";
            de_Ngaytuyendung.Text = "";
            lke_DV.EditValue = "";
            rdg_Dangvien.SelectedIndex = 0;
            rdg_Nhapngu.SelectedIndex = 0;


            btn_themAnh.Enabled = false;
            //btn_xoaAnh.Enabled = false;

            txt_Ma_CB.Enabled = false;
            txt_Hoten.Enabled = false;
            txt_Tengoikhac.Enabled = false;
            rdg_Gioitinh.Enabled = false;
            de_Namsinh.Enabled = false;
            txt_Noisinh.Enabled = false;
            txt_Quequan.Enabled = false;
            txt_SoCMND.Enabled = false;
            de_NgaycapCMND.Enabled = false;
            txt_Noicap.Enabled = false;
            txt_SoBHXH.Enabled = false;
            rdg_Thuongbinh.Enabled = false;
            txt_Hokhau.Enabled = false;
            txt_Choo.Enabled = false;
            lke_Dantoc.Enabled = false;
            lke_Tongiao.Enabled = false;
            txt_Suckhoe.Enabled = false;
            txt_Nghenghiep.Enabled = false;
            de_Ngaytuyendung.Enabled = false;
            lke_DV.Enabled = false;
            rdg_Dangvien.Enabled = false;
            rdg_Nhapngu.Enabled = false;
        }

       

        private void de_Ngaytuyendung_EditValueChanged(object sender, EventArgs e)
        {
            //DateTime NgayTuyenDung = de_Ngaytuyendung.DateTime;
            //DateTime NgaySinh = de_Namsinh.DateTime;
            //if (NgayTuyenDung.Year - NgaySinh.Year < 0)
            //{
            //    MessageBox.Show("Ngày tuyển dụng phải lớn hơn ngày sinh!");
            //    de_Ngaytuyendung.Focus();
            //    //demtime = 0;
            //}
        }

        private void de_NgaycapCMND_EditValueChanged(object sender, EventArgs e)
        {
            
            //DateTime NgayCapCMND = de_NgaycapCMND.DateTime;
            //DateTime NgaySinh = de_Namsinh.DateTime;
            //if (NgayCapCMND.Year - NgaySinh.Year <0)
            //{
            //    MessageBox.Show("Ngày cấp chứng minh phải lớn hơn ngày sinh!");
            //    de_NgaycapCMND.Focus();
            //    //demtime = 0;
            //}
        }

       

        private void btn_themAnh_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "JPG|*.JPG|PNG|*.PNG|BITMAP|*.BMP|ALL FILE|*.*";
            if (op.ShowDialog() == DialogResult.OK)
            {
                pte_AnhCB.Image = Image.FromFile(op.FileName);
                tempPath = op.FileName;
            }
        }

        private void de_Namsinh_EditValueChanged(object sender, EventArgs e)
        {
            //DateTime t = new DateTime();
            //MessageBox.Show(t.ToString());
        }

        private void txt_SoBHXH_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void btn_themDV_Click(object sender, EventArgs e)
        {
            Frm_ThemDMDV frm = new Frm_ThemDMDV();
            frm.ShowDialog();
            lke_DV.Properties.DataSource = sv.getDMDV();
        }

        private void btn_themDT_Click(object sender, EventArgs e)
        {
            Frm_DT frm = new Frm_DT();
            frm.ShowDialog();
            lke_Dantoc.Properties.DataSource = sv.getDT();
        }

        private void btn_themTG_Click(object sender, EventArgs e)
        {
            Frm_TG frm = new Frm_TG();
            frm.ShowDialog();
            lke_Tongiao.Properties.DataSource = sv.getTG();
        }

        private void ck_coGDCS_CheckedChanged(object sender, EventArgs e)
        {
            if (ck_coGDCS.Checked == false)
            {
                lke_Gdchinhsach.Enabled = false;
                lke_Gdchinhsach.EditValue = null;
            }
            else
            {
                lke_Gdchinhsach.Enabled = true;
                if (lke_Gdchinhsach.EditValue == null)
                {
                    //MessageBox.Show("Vui lòng chọn Diện GDCS!");
                }

            }
        }









        
    }
}
